<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="br.org.group.android.dao.FortunesDAO" %>
<%@ page import="br.org.group.android.dao.PMF" %>

<html>
  <body>
<%

		int id = 1;
		List<FortunesDAO> fortunes;
		PersistenceManager pm = PMF.get().getPersistenceManager();
try {
		pm = PMF.get().getPersistenceManager();
		
		if((request.getParameter("id") != null) && (request.getParameter("id").trim() != "")){
			id = Integer.parseInt((String)request.getParameter("id").trim());
		}
		if(id < 0 ){
			id = id * -1;
		}

	   String query = "select from " + FortunesDAO.class.getName() + " where id == "+id;
	    fortunes = (List<FortunesDAO>) pm.newQuery(query).execute();
	    
	    if (fortunes.isEmpty()) {
	    	response.getWriter().println("Fortune não existente!");
	    	return;
	    }else{
	    	if(fortunes.size() == 1){
	    		response.getWriter().println("<h2>Fortune:</h2>"+fortunes.get(0).getMensagem().getValue());
	    		response.getWriter().println("<br><h2>Autor:</h2>"+fortunes.get(0).getAutor().trim());
	    	}
	    }
  } finally {
       pm.close();
 }


%>

    <form action="/client.jsp?id=<%= ++id%>"
		 method="post">
      <div><input type="submit" value="Nova Fortune" /></div>
    </form>

  </body>
</html>